package com.turnengine.client.local.group.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.group.bean.GroupSerializer;
import com.turnengine.client.local.group.bean.IGroup;
import java.io.IOException;

/**
 * The Add Child Group Data Serializer.
 */
public class AddChildGroupDataSerializer extends AbstractCommandDataSerializer<IGroup> implements IAddChildGroupDataSerializer {

	/**
	 * Creates a new Add Child Group Data Serializer.
	 */
	public AddChildGroupDataSerializer() {
		super(AddChildGroup.COMMAND_ID);
	}

	@Override
	public IGroup readReturnValue(IDataReader reader) throws IOException {
		IGroup value = new GroupSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IGroup value) throws IOException {
		new GroupSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IGroup> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		String name = reader.readString(true);
		String parentName = reader.readString(true);
		String factionName = reader.readString(true);

		// Create object
		AddChildGroup object = new AddChildGroup();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setName(name);
		object.setParentName(parentName);
		object.setFactionName(factionName);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IGroup> argument) throws IOException {
		AddChildGroup object = (AddChildGroup) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		String name = object.getName();
		String parentName = object.getParentName();
		String factionName = object.getFactionName();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeString(name, true);
		writer.writeString(parentName, true);
		writer.writeString(factionName, true);
	}

	@Override
	public IExecutableCommandResponse<IGroup> newResponse() {
		return new AddChildGroupResponse();
	}
}
